package com.northone.statistics.adapter;

import com.northone.statistics.R;
import com.northone.statistics.database.SaintDataBaseHelper;
import com.northone.statistics.utils.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class SaintCursorAdapter extends CursorAdapter {

	private SQLiteDatabase database;

	public SaintCursorAdapter(Context context, Cursor c, boolean autoRequery, SQLiteDatabase database) {
		super(context, c, autoRequery);
		this.database = database;
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.saint_item, parent, false);

		holder.iv_avatar = (ImageView) view.findViewById(R.id.iv_avatar);
		holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
		holder.tv_sex = (TextView) view.findViewById(R.id.tv_sex);
		holder.tv_type = (TextView) view.findViewById(R.id.tv_type);
		holder.tv_baptism = (TextView) view.findViewById(R.id.tv_baptism);
		holder.tv_phone = (TextView) view.findViewById(R.id.tv_phone);
		holder.tv_wechat = (TextView) view.findViewById(R.id.tv_wechat);

		holder.callPhone = (ImageView) view.findViewById(R.id.callPhone);
		holder.deleteSaint = (ImageView) view.findViewById(R.id.deleteSaint);

		view.setTag(holder);

		return view;
	}

	@Override
	public void bindView(View view, final Context context, Cursor cursor) {
		ViewHolder holder = (ViewHolder) view.getTag();
		
		String sex = cursor.getString(cursor.getColumnIndex("sex"));
		holder.iv_avatar.setImageResource(sex.equals("弟兄") ? R.drawable.head_1 : R.drawable.head_3);
		holder.tv_name.setText(cursor.getString(cursor.getColumnIndex("name")));
		holder.tv_sex.setText(sex);
		holder.tv_type.setText(cursor.getString(cursor.getColumnIndex("type")));
		holder.tv_baptism.setText(cursor.getString(cursor.getColumnIndex("baptism")));
		final String phoneStr = cursor.getString(cursor.getColumnIndex("phone"));
		holder.tv_phone.setText(phoneStr);
		holder.tv_wechat.setText(cursor.getString(cursor.getColumnIndex("wechat")));
		
		holder.iv_avatar.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ToastUtil.showToast(context, "click-avatar");
				//TODO
			}
		});
		holder.callPhone.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				long phoneNumber = Long.parseLong(phoneStr.substring(phoneStr.indexOf(":") + 1));
				Intent intent = new Intent(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:" + phoneNumber));
				context.startActivity(intent);
			}
		});
		holder.deleteSaint.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				database.delete(SaintDataBaseHelper.TABLE_NAME_BASE, "phone=?",
						new String[] { phoneStr });
				Cursor cursor = database.query(SaintDataBaseHelper.TABLE_NAME_BASE, null, null, null, null,
						null, null);
				changeCursor(cursor);
			}
		});
	}

	public class ViewHolder {
		public ImageView iv_avatar, callPhone, deleteSaint;
		public TextView tv_name, tv_sex, tv_type, tv_baptism, tv_phone, tv_wechat;
	}
}
